package com.yc.booktable.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.booktable.biz.AdminUserBiz;
import com.yc.booktable.biz.BizException;
import com.yc.booktable.entity.AdminUser;
import com.yc.booktable.mapper.AdminUserMapper;
import com.yc.booktable.service.impl.AdminUserServiceImpl;
import com.yc.booktable.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserAction {
    @Resource
    private AdminUserMapper adminUserMapper;
    @Resource
    private AdminUserServiceImpl adminUserService;
    @Resource
    private AdminUserBiz adminUserBiz;

    /**
     * 实现分页
     */
    @RequestMapping("user")
    public String food(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer PageNum,HttpSession session){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null|| user.getState()==1){
            return "redirect:/login";
        }
        model.addAttribute("user",user);
        Page page = new Page(PageNum,10);
        page = adminUserMapper.selectPage( page,null);
        // List<AdminUser> adminUsers = adminUserMapper.selectList(null);
        model.addAttribute("adminuser",page);
        return "user";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String move(@PathVariable("id") int id){
        try {
            System.out.println("删除");
            adminUserService.removeById(id);
            return "redirect:/user";
        } catch ( Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * 添加成员
     * @param model
     * @return
     */
    @RequestMapping("maps")
    public String typesInput(Model model,HttpSession session){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null|| user.getState()==1){
            return "redirect:/login";
        }
        model.addAttribute("user",user);
        model.addAttribute("adminuser",new AdminUser());
        return "maps";
    }
    @PostMapping("/addPage")
    public String add(AdminUser adminUser, HttpSession session) throws BizException {
        try {
            adminUserBiz.save(adminUser);
            return "redirect:/user";
        } catch (BizException e) {
            e.printStackTrace();
            session.setAttribute("msg",e.getMessage());
            return "forward:/error";
        }
    }
    /**
     * 修改信息
     */
    @GetMapping("/queryau")
    public String queryau(Integer id,Model model){
        AdminUser adminser1 = adminUserService.getById(id);
        model.addAttribute("adminuser1",adminser1);
        return "editau";
    }
    @PostMapping("update")
    public String update(AdminUser adminuser1,HttpSession session) {
        try {
           adminUserBiz.save(adminuser1);
            return "redirect:/user";
        } catch (BizException e) {
            e.printStackTrace();
            session.setAttribute("msg",e.getMessage());
            return "forward:/error";
        }
    }




}
